<?php
/**
 * @author Brian Voss
 */
class GroovesharkController extends Social_Oauth_BaseController {
	/**
	 * @var String 
	 */
	protected $serviceKey = 'grooveshark';
	/**
	 * @var Grooveshark_Api 
	 */
	protected $api;
	/**
	 * Initialization 
	 */
	public function init()
	{
		parent::init();
		$api_key = $this->config->key;
		$api_secret = $this->config->secret;
		$this->api = new Grooveshark_Api($api_key, $api_secret);
		
		if ( $this->isAuthenticated())
		{
			$this->api->setSession($this->session->sessionId);
		}
		else
		{
			// Start a session
			$session = $this->api->startSession();
			$this->session->sessionId = $session;
			$user = new Grooveshark_User();
			// Log me in
			$user->setUsername($this->config->username);
			$user->setTokenFromPassword($this->config->password);
			$user = $this->api->authenticateUser($user);
			$this->session->profile = serialize($user);
		}
	}
	/**
	 * Default Page 
	 */
	public function indexAction()
	{
		$user = unserialize($this->session->profile);
		var_dump($user);
	}
	/**
	 * Start authentication 
	 */
	public function startauthAction()
	{
		echo "ok";
	}
	/**
	 * Complete authentication
	 */
	public function finishauthAction()
	{
		if ( $this->isAuthenticated() )
		{
			echo "worked!";
		}
		else
		{
			echo "didn't work";
		}
	}
	/**
	 * Cancel Authentication 
	 */
	public function cancelauthAction()
	{
		$this->api->endSession();
		$this->session->sessionId = null;
	}
	/**
	 * Helper method for determining authentication status
	 * @return bool 
	 */
	protected function isAuthenticated()
	{
		return !(empty($this->session->sessionId));
	}
	
	
	public function favoritesAction()
	{
		$this->api->getUserInfo();
	}
	
}